package org.kaer.election.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import org.kaer.election.R;
import org.kaer.election.bean.Candidate;
import org.kaer.election.utils.BitmapStream;
import org.kaer.election.utils.LogUtils;

import java.util.List;

/**
 * Created by lizj on 2016/7/25.
 */
public class SearchCandidateAdapter extends RecyclerView.Adapter<SearchCandidateAdapter.ViewHolder> {
    private Context mContext;
    private List<Candidate> canList;
    private Typeface numTf;

    public SearchCandidateAdapter(Context mContext, List<Candidate> canList) {
        this.mContext = mContext;
        this.canList = canList;
        for (Candidate item : canList) {
            LogUtils.i("SearchCandidateAdapter " + item.toString());
        }
        numTf = Typeface.createFromAsset(mContext.getAssets(), "fonts/number.otf");
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_candidate_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder mHolder, int position) {
        Candidate info = canList.get(position);
        mHolder.tvName.setText(info.voterName);
        mHolder.tvCardNum.setTypeface(numTf);
        mHolder.tvCardNum.setText(info.voterCertNumber.substring(0, 4) + "********"
                + info.voterCertNumber.substring(info.voterCertNumber.length() - 4));
        mHolder.tvAddr.setText(info.voterAddress);

        if (TextUtils.isEmpty(info.getPositionName())) {
            mHolder.llBg.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            mHolder.tvPosition.setVisibility(View.GONE);
            mHolder.tvPosition.setText(null);

        } else {
            mHolder.llBg.setBackgroundColor(mContext.getResources().getColor(R.color.election_bg));
            mHolder.tvPosition.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(mHolder.tvPosition.getText())) {
                //如果setText前该项为空，展示动画效果
                AnimatorSet mSet = new AnimatorSet();
                ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mHolder.tvPosition, "scaleX", 1f, 1.5f, 1f);
                ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(mHolder.tvPosition, "scaleY", 1f, 1.5f, 1f);
                mSet.playTogether(scaleXAnimator, scaleYAnimator);
                mSet.setInterpolator(new AccelerateDecelerateInterpolator());
                mSet.setDuration(1000);
                mSet.start();

            }
            mHolder.tvPosition.setText(info.getPositionName());
        }
        //照片信息处理
        if (info.voterPicture != null && info.voterPicture.length() > 100) {
            Bitmap bitmap = BitmapStream.stringtoBitmap(info.voterPicture);
            LogUtils.i(bitmap.getWidth() + "///" + bitmap.getHeight());
            mHolder.ivHead.setImageBitmap(BitmapStream.scaleBitmap(bitmap, 73, 77)); // 候选人照片base64编码

        } else {
            if ("女".equals(info.voterGender))
                mHolder.ivHead.setImageResource(R.drawable.person_female);
            else
                mHolder.ivHead.setImageResource(R.drawable.person);

        }
    }

    @Override
    public int getItemCount() {
        return canList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private FrameLayout llBg;
        private ImageView ivHead;
        private TextView tvName;
        private TextView tvAddr;
        private TextView tvCardNum;
        private TextView tvPosition;

        public ViewHolder(View itemView) {
            super(itemView);
            ivHead = (ImageView) itemView.findViewById(R.id.layout_candiate_ivHead);
            tvName = (TextView) itemView.findViewById(R.id.layout_candiate_tvName);
            tvAddr = (TextView) itemView.findViewById(R.id.layout_candiate_tvAddr);
            tvCardNum = (TextView) itemView.findViewById(R.id.layout_candiate_tvCardNum);
            tvPosition = (TextView) itemView.findViewById(R.id.layout_candidate_tvPosition);
            llBg = (FrameLayout) itemView.findViewById(R.id.layout_canItem_llBg);
        }
    }
}
